package com.zzl.alligator.management.system.service.impl;


import com.zzl.alligator.common.constant.SystemConstants;
import com.zzl.alligator.management.system.mapper.SysUserAssistMapper;
import com.zzl.alligator.management.system.mapper.SysUserNoticeMapper;
import com.zzl.alligator.management.system.domain.SysUserNotice;
import com.zzl.alligator.management.system.service.ISysUserNoticeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
/**
 * 用户通知表(SysUserNotice)服务层
 *
 * @author zhouziliang
 * @since 2020-07-15
 */
@Service
public class SysUserNoticeService implements ISysUserNoticeService {
    
    @Autowired
    private SysUserNoticeMapper sysUserNoticeMapper;

    @Autowired
    private SysUserAssistMapper sysUserAssistMapper;
    
    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public List<SysUserNotice> findSysUserNoticeById(Long userId) {
        return sysUserNoticeMapper.findSysUserNoticeById(userId);
    }

   /**
     * 通过实体作为筛选条件查询
     *
     * @param sysUserNotice 实例对象
     * @return 对象列表
     */
    @Override
    public List<SysUserNotice> findSysUserNoticeList(SysUserNotice sysUserNotice) {
        return sysUserNoticeMapper.findSysUserNoticeList(sysUserNotice);
    }

    /**
     * 新增数据
     *
     * @param sysUserNotice 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysUserNotice(SysUserNotice sysUserNotice) {
        return sysUserNoticeMapper.insertSysUserNotice(sysUserNotice);
    }

    /**
     * 修改数据
     *
     * @param sysUserNotice 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserNotice(SysUserNotice sysUserNotice) {
        return sysUserNoticeMapper.updateSysUserNotice(sysUserNotice);
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Long userId) {
        return sysUserNoticeMapper.deleteById(userId);
    }

    /**
     * 用户消息已读
     *
     * @param userId   用户ID
     * @param noticeId 消息ID
     * @return 影响条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNoticeRead(Long userId, Integer noticeId) {
        SysUserNotice sysUserNotice = new SysUserNotice();
        sysUserNotice.setNoticeId(noticeId);
        sysUserNotice.setUserId(userId);
        sysUserNotice.setReadStatus(SystemConstants.STATUS_READ);
        sysUserNotice.setReadTime(LocalDateTime.now());
        sysUserAssistMapper.userPersonNoticeSub(userId);
        return sysUserNoticeMapper.updateSysUserNotice(sysUserNotice);
    }

    /**
     * 用户所有已读取
     *
     * @param userId 用户ID
     * @return 影响条数
     */
    @Override
    public int readAll(Long userId) {
        SysUserNotice un = new SysUserNotice();
        un.setReadTime(LocalDateTime.now());
        un.setReadStatus(SystemConstants.STATUS_READ);
        un.setUserId(userId);
        sysUserAssistMapper.userPersonNoticeClear(userId);
        return sysUserNoticeMapper.updateSysUserNoticeByUserId(un);
    }
}